package com.kitty.game.pet.service;


import com.alibaba.fastjson.JSON;
import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.difu.domin.*;
import com.kitty.game.difu.service.GhostPeiYuan;
import com.kitty.game.difu.service.GhostPeiYuanResult;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.pet.message.RespPetInfo;
import com.kitty.game.pet.message.vo.PetInfo;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

import static java.lang.Thread.sleep;

@Service
public class PetPeiYuanService {
    @Autowired
    protected PetService petService;
    @Autowired
    RoleService roleService;
    @Autowired
    RedisService redisService;
    private static List<String> list = Arrays.asList("max_life","phy_power","mag_power","speed","def");
    public void startPeiYuan(IoSession session, GhostPeiYuan ghostPeiYuan){
        Role role = SessionUtils.getRoleBySession(session);
        int pet_no = ghostPeiYuan.getPet_no();
        Pet zhuPet = null;
        Map<Integer, Pet> allPets = role.getPetBox().getAllPets();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if(pet.getPosition() == pet_no) {
                zhuPet = pet;
                break;
            }
        }
        if(zhuPet != null){
            boolean flag = false;
            int peiyuan_data_state = zhuPet.getPeiyuan_data_state();
            int peiyuan_data_level = zhuPet.getPeiyuan_data_level();
            int peiyuan_data_stage = zhuPet.getPeiyuan_data_stage();
            int xiaohaoGuiQi = getPeiYuanUse("xiaohao",peiyuan_data_stage);
            if(xiaohaoGuiQi >role.getGuiqi()){
                //鬼气不够
                MessagePusher.pushMessage(session, new RespMsg("当前鬼气不足以提升！"));
            }else{
                if(peiyuan_data_stage !=0 && peiyuan_data_stage % 11==0){
                    MessagePusher.pushMessage(session, new RespMsg("已达上限！"));
                    return;
                }
                SpringUtils.getBean(RoleService.class).addAchieveScore(role,"201008");
                String s = this.sendPeiYuanPanel(role, zhuPet,true);
                roleService.subGuiQi(role,xiaohaoGuiQi,true);
                String shuxingcao = "";
                if("max_life".equals(s)){
                    shuxingcao = "气血";
                }else if("phy_power".equals(s)){
                    shuxingcao = "物伤";
                }else if("mag_power".equals(s)){
                    shuxingcao = "法伤";
                }else if("speed".equals(s)){
                    shuxingcao = "速度";
                }else if("def".equals(s)){
                    shuxingcao = "防御";
                }
                String message = "培元成功, 你的#R"+zhuPet.getName()+"#n激活了#R"+shuxingcao+"#n属性槽！";
                MessagePusher.pushMessage(session, new RespMsg(message));
                GhostPeiYuanResult ghostPeiYuanResult = new GhostPeiYuanResult();
                ghostPeiYuanResult.setAttrib_key(s);
                GhostPeiYuanBasic ghostPeiYuanBasic = zhuPet.getGhostPeiYuanBasic();
                if(ghostPeiYuanBasic.getDef() && ghostPeiYuanBasic.getMag_power() && ghostPeiYuanBasic.getMax_life() && ghostPeiYuanBasic.getPhy_power() && ghostPeiYuanBasic.getSpeed()){
                    flag = true;
                    if((peiyuan_data_level+1) % 5==0){
                        peiyuan_data_stage += 1;
                        if(peiyuan_data_stage % 11==0){
                            MessagePusher.pushMessage(session, new RespMsg("已达上限！"));
                            return;
                        }
                        ghostPeiYuanResult.setStageUp((byte)1);
                        ghostPeiYuanResult.setLevelUp((byte)1);
                        peiyuan_data_level = 0;
                        //   ghostPeiYuanResult.setLevelUp((byte)0);
                    }else{
                        //   ghostPeiYuanResult.setStageUp((byte)1);
                        peiyuan_data_level+=1;
                        if(peiyuan_data_stage != 0 && peiyuan_data_stage % 10==0 && peiyuan_data_level==4){
                            MessagePusher.pushMessage(session, new RespMsg("已达上限！"));
                            return;
                        }
                        ghostPeiYuanResult.setLevelUp((byte)1);
                    }
                    //发送全部为空的协议
                    //this.sendPeiYuanPanel(role, zhuPet,true);
                    //初始化 属性点
                    zhuPet.setGhostPeiYuanBasic(new GhostPeiYuanBasic());
                    zhuPet.setPeiyuanCurrentInfo(new HashMap<>());
                    ghostPeiYuanResult.setAttrib_key("");
                    zhuPet.setPeiyuan_data_level(peiyuan_data_level);
                    zhuPet.setPeiyuan_data_stage(peiyuan_data_stage);
                    zhuPet.setPeiyuan_data_state(peiyuan_data_state);
                    //zhuPet.setPeiyuan_ghost_gas(1000);
                    petService.refreshPetInfo(role, zhuPet);
                }
                ghostPeiYuanResult.setPet_no((short)pet_no);
                ghostPeiYuanResult.setResult((byte)1);
                MessagePusher.pushMessage(session, ghostPeiYuanResult);
                if(flag){
                    try {
                        sleep(2500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
//                    respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_CLOSE_DLG); //关闭对话框
//                    respGeneralNotify.setValue("GhostPetPeiYuanDlg");
//                    MessagePusher.pushMessage(session, respGeneralNotify);
//                    RespGeneralNotify respGeneralNotify2 = new RespGeneralNotify();
//                    respGeneralNotify2.setNotify((short) NotifyModules.NOTIFY_OPEN_DLG); //打开对话框
//                    respGeneralNotify2.setValue("GhostPetPeiYuanDlg");
//                    MessagePusher.pushMessage(session, respGeneralNotify2);
                    sendPeiYuanPanel(role, zhuPet,false);
                }
            }

        }
    }
    public  String  sendPeiYuanPanel(Role role, Pet zhuPet,boolean init){
        RespPetInfo respPetInfo = new RespPetInfo();
        List<PetInfo> petInfos = new ArrayList<>();
        PetInfo petInfo = new PetInfo();
        petInfo.setPetId(zhuPet.getId());
        petInfo.setPosition(zhuPet.getPosition());
        PeiYuanFieldOne basicField = new PeiYuanFieldOne();
        String type = "";
        if(init){
            type = setPeiYuanInfo(role,zhuPet,basicField);
        }
        getPeiYuanInfo(zhuPet,basicField);


        List<EquipField> petFields = new ArrayList<>();
        petFields.add(basicField);
        petInfo.setList(petFields);
        petInfos.add(petInfo);
        respPetInfo.setPetInfos(petInfos);
        MessagePusher.pushMessage(role, respPetInfo);
        return type;
    }
    //查询培元加成信息
    public  void getPeiYuanInfo(Pet pet, PeiYuanFieldOne basicField){
        GhostPeiYuanBasic ghostPeiYuanBasic = pet.getGhostPeiYuanBasic();
        if(ghostPeiYuanBasic == null){
            ghostPeiYuanBasic = new GhostPeiYuanBasic();
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
        }

        List<PeiYuanFieldAdd> peiYuanFieldAdds = new ArrayList<>();
        Map<String, Integer> addInfo = pet.getPeiyuan_add_info();
        for (Map.Entry<String, Integer> stringIntegerEntry : addInfo.entrySet()) {
            PeiYuanFieldAdd peiYuanFieldAdd = new PeiYuanFieldAdd();
            peiYuanFieldAdd.setAddlabel(stringIntegerEntry.getKey());
            peiYuanFieldAdd.setAddlabelVal(stringIntegerEntry.getValue());
            peiYuanFieldAdds.add(peiYuanFieldAdd);
        }
        basicField.setPeiYuanFieldAddList(peiYuanFieldAdds);
        //点亮的属性槽
        List<PeiYuanFieldAddLabel> labels = new ArrayList<>();
        Map<String, Integer> peiyuanCurrentInfo = pet.getPeiyuanCurrentInfo();
        for (Map.Entry<String, Integer> stringIntegerEntry : peiyuanCurrentInfo.entrySet()) {
            PeiYuanFieldAddLabel peiYuanFieldAddLabel = new PeiYuanFieldAddLabel();
            peiYuanFieldAddLabel.setAddlabel(stringIntegerEntry.getKey());
            labels.add(peiYuanFieldAddLabel);
        }
        basicField.setPeiYuanFieldList(labels);
        //每个属性槽内增加的数值
        List<PeiYuanFieldAdd> curAdds = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            int sumPower = getPeiYuanUse(list.get(i),pet.getPeiyuan_data_stage());
            PeiYuanFieldAdd peiYuanFieldAdd = new PeiYuanFieldAdd();
            peiYuanFieldAdd.setAddlabel(list.get(i));
            peiYuanFieldAdd.setAddlabelVal(sumPower);
            curAdds.add(peiYuanFieldAdd);
        }
        basicField.setPeiYuanFieldCurList(curAdds);
        int stage = pet.getPeiyuan_data_stage();
        if((pet.getPeiyuan_data_level()+1) % 5 == 0 && curAdds.size()==5){
            stage += 1;
        }
//        if(curAdds.size()==5){
//            basicField.setPeiYuanFieldList(new ArrayList<>());
//            basicField.setPeiYuanFieldCurList(new ArrayList<>());
//        }
        int xiaohaoGuiQi = getPeiYuanUse("xiaohao",stage);
        basicField.setCostGhostGas(xiaohaoGuiQi);
    }
    //设置培元加成信息
    public  String setPeiYuanInfo(Role role,Pet pet, PeiYuanFieldOne basicField){
        GhostPeiYuanBasic ghostPeiYuanBasic = pet.getGhostPeiYuanBasic();
        if(ghostPeiYuanBasic == null){
            ghostPeiYuanBasic = new GhostPeiYuanBasic();
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
        }
        Boolean active = ghostPeiYuanBasic.getMax_life();
        if(!active){
            setPeiYuanAddMap(pet,"max_life");
            setPeiYuanCurrentMap(pet,"max_life");
            ghostPeiYuanBasic.setMax_life(true);
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
            return "max_life";
        }
        active = ghostPeiYuanBasic.getPhy_power();
        if(!active){
            setPeiYuanAddMap(pet,"phy_power");
            setPeiYuanCurrentMap(pet,"phy_power");
            ghostPeiYuanBasic.setPhy_power(true);
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
            return "phy_power";
        }
        active = ghostPeiYuanBasic.getMag_power();
        if(!active){
            setPeiYuanAddMap(pet,"mag_power");
            setPeiYuanCurrentMap(pet,"mag_power");
            ghostPeiYuanBasic.setMag_power(true);
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
            return "mag_power";
        }
        active = ghostPeiYuanBasic.getSpeed();
        if(!active){
            setPeiYuanAddMap(pet,"speed");
            setPeiYuanCurrentMap(pet,"speed");
            ghostPeiYuanBasic.setSpeed(true);
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
            return "speed";
        }
        active = ghostPeiYuanBasic.getDef();
        if(!active){
            setPeiYuanAddMap(pet,"def");
            setPeiYuanCurrentMap(pet,"def");
            ghostPeiYuanBasic.setDef(true);
            pet.setGhostPeiYuanBasic(ghostPeiYuanBasic);
            return "def";
        }
        return "";
    }
    //设置培元总加成
    public void setPeiYuanAddMap(Pet pet,String type){
        Map<String, Integer> addInfo = pet.getPeiyuan_add_info();
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(type)){
            int sumPower = getPeiYuanUse(type,pet.getPeiyuan_data_stage());
            if(!addInfo.containsKey(type)){
                addInfo.put(type,sumPower);
            }else{
                addInfo.put(type,addInfo.get(type)+sumPower);
            }
        }
    }
    //设置培元当前加成
    public void setPeiYuanCurrentMap(Pet pet,String type){
        Map<String, Integer> addInfo = pet.getPeiyuanCurrentInfo();
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(type)){
            int sumPower = getPeiYuanUse(type,pet.getPeiyuan_data_stage());
            if(!addInfo.containsKey(type)){
                addInfo.put(type,sumPower);
            }else{
                addInfo.put(type,addInfo.get(type)+sumPower);
            }
        }
    }

    public  int  getPeiYuanUse(String type,Integer jie){
        String key = "";
        int num = 0;
        switch (jie.intValue()){
            case 1:
                key="peiyuan_yijie";
                break;
            case 2:
                key="peiyuan_erjie";
                break;
            case 3:
                key="peiyuan_sanjie";
                break;
            case 4:
                key="peiyuan_sijie";
                break;
            case 5:
                key="peiyuan_wujie";
                break;
            case 6:
                key="peiyuan_liujie";
                break;
            case 7:
                key="peiyuan_qijie";
                break;
            case 8:
                key="peiyuan_bajie";
                break;
            case 9:
                key="peiyuan_jiujie";
                break;
            case 10:
                key="peiyuan_shijie";
                break;
            case 11:
                key="peiyuan_shijie";
                break;
            case 12:
                key="peiyuan_shijie";
                break;
        }
        String magnification = DataCache.REDIS_DATA.get(key).toString();
        if("xiaohao".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_xiaohao").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }else if("max_life".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_qixue").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }else if("phy_power".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_wugong").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }else if("mag_power".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_fashang").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }else if("speed".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_sudu").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }else if("def".equals(type)){
            String use = DataCache.REDIS_DATA.get("peiyuan_fangyu").toString();
            num = (int)(Double.parseDouble(use) * Double.parseDouble(magnification));
        }
        return num;
    }
    public static void main(String[] args) {
        PetYuanCurStateInfoBuilder petYuanCurStateInfoBuilder = PetYuanCurStateInfoBuilder.builder() ;
        Object o = JSON.toJSON(petYuanCurStateInfoBuilder);
        System.out.println(o.toString());
        System.out.println( String.valueOf(petYuanCurStateInfoBuilder));
//    Integer a= new Integer(1);
//    Integer b =new Integer(1);
//    System.out.println(a==b);
//    System.out.println(10>>1);
    }

}
